/*
 * GridBox.java
 *
 * Created on February 8, 2005, 2:22 AM
 */

package net.jxta.myjxta.plugins.tictactoe;

import net.jxta.logging.Logging;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * @author Jeff Moore
 */
public final class TicTacToeGridBox extends JPanel {

    static final Logger LOG = Logger.getLogger(TicTacToeGridBox.class.getName());

    private EmptyBorder emptyBorder = null;

    private LineBorder lineBorder = null;

    private LineBorder normalBorder = null;

    private final Dimension maxDimension = new Dimension(68, 68);
    /**
     * either LOCAL_PLAYER or REMOTE_PLAYER
     */
    private int player = 0;

    private String id = null;

    private boolean available = true;

    private TicTacToeDialogView view = null;

    private Image image;

    private Insets insets = null;

    boolean blinking = false;

    public TicTacToeGridBox(String id, TicTacToeDialogView view) {

        super();

        LOG.setLevel(Level.INFO);

        this.id = id;

        this.view = view;

        insets = new Insets(0, 0, 0, 0);

        this.emptyBorder = new EmptyBorder(2, 2, 2, 2);

        this.lineBorder = new LineBorder(Color.RED, 2);

        this.normalBorder = new LineBorder(Color.BLUE, 1);

        setOpaque(false);

        setBorder(normalBorder);

        setPlayerIcon(view.getIconBlank());

        setVisible(true);


    }

    public Insets getInsets() {
        return insets;
    }

    public Insets getInsets(Insets insets) {
        return getInsets();
    }

    public boolean isAvailable() {

        return available;

    }

    public void setAvailable(boolean available) {

        this.available = available;

    }

    public String getId() {

        return this.id;

    }

    public void reset() {
        setPlayerIcon(view.getIconBlank());
        setAvailable(true);
    }

    public int getPlay() {

        return this.player;

    }


    public Dimension getMaximunSize() {

        return maxDimension;

    }


    public Dimension getMinimumSize() {

        return maxDimension;

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(this.image, 0, 0, this);


    }

    public Dimension getPreferredSize() {

        return maxDimension;

    }


    private void setPlayerIcon(final ImageIcon icon) {
        this.image = icon.getImage().getScaledInstance(68, 68, Image.SCALE_SMOOTH);
        repaint();
        /*
        EventQueue.invokeLater (new Runnable () {
            public void run () {
                
                setIcon (icon);
                
            }
        });
         **/
    }


    protected void setBlink() {

        if (blinking) {

            blink(blinking = false);
        } else {
            blink(blinking = true);
        }
    }

    protected void blink(final boolean blink) {


        EventQueue.invokeLater(new Runnable() {
            public void run() {

                if (blink) {
                    setBorder(lineBorder);
                } else {
                    setBorder(normalBorder);
                }


            }
        });


    }

    public void makeMove(int player) {
        if (isAvailable()) {

            this.player = player;

            ImageIcon icon = null;

            if (player == TicTacToeGameControl.LOCAL_PLAYER) {

                icon = view.getLocalPlayerIcon();

            } else if (player == TicTacToeGameControl.REMOTE_PLAYER) {
                if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
                    LOG.info("remotePlayerIcon is " + view.getRemotePlayerIcon());
                }
                icon = view.getRemotePlayerIcon();

            } else {

                icon = view.getIconBlank();

            }

            setPlayerIcon(icon);
        }
    }


}
